package br.com.emmis.tercons;

import android.app.Activity;
import android.app.Fragment;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.google.analytics.tracking.android.EasyTracker;

import br.com.emmis.tercons.controller.TercoPlayer;
import br.com.emmis.tercons.controller.TercoStateMachine;
import br.com.emmis.tercons.controller.TercoStateMachineListener;
import br.com.emmis.tercons.model.Config;
import br.com.emmis.tercons.model.interfaces.Pray;

/**
 * A fragment with a Google +1 button.
 * Activities that contain this fragment must implement the
 * {@link br.com.emmis.tercons.RezarFragment.OnRezarFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link RezarFragment#newInstance} factory method to
 * create an instance of this fragment.
 *
 */
public class RezarFragment extends Fragment implements View.OnClickListener, TercoStateMachineListener {

    private TercoStateMachine mTercoStateMachine;
    private TextView tvPray;
    private TextView tvTitle;
    private boolean mIsAutomatic;
    private boolean mEnableSound;
    private TercoPlayer mTercoPlayer;
    private EasyTracker mEasyTracker;
    private Config mConfig;


    private OnRezarFragmentInteractionListener mListener;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @return A new instance of fragment RezarFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static RezarFragment newInstance() {
        RezarFragment fragment = new RezarFragment();
        return fragment;
    }

    public RezarFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mConfig = Config.getInstance(getActivity().getApplicationContext());
        mEasyTracker = EasyTracker.getInstance(getActivity().getApplicationContext());

        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        boolean autoPray = mConfig.ismAutoPray();
		mEnableSound = mConfig.ismEnableSound();
        boolean keepScreenOn = mConfig.ismKeepScreenOn();

        ((ActionBarActivity)getActivity()).getSupportActionBar().hide();

        mTercoStateMachine = new TercoStateMachine();
        mTercoStateMachine.addTercoStateMachineListener(this);
        mTercoStateMachine.setmAutomatic(autoPray);

        if(keepScreenOn) {
            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
        else {
            getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        mEasyTracker.activityStop(getActivity());
    }

    @Override
    public void onStart() {
        super.onStart();
        mEasyTracker.activityStart(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_rezar, container, false);

        view.findViewById(R.id.btnContinue).setOnClickListener(this);
        tvPray = (TextView) view.findViewById(R.id.tvPray);
        tvTitle = (TextView) view.findViewById(R.id.tvTitulo);

        if(mTercoStateMachine.ismAutomatic()){
            view.findViewById(R.id.btnContinue).setEnabled(false);
        }
		
		mTercoPlayer = new TercoPlayer(getActivity().getApplicationContext());
        mTercoStateMachine.startTerco();

        return view;
    }


    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnRezarFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }


    @Override
    public void onDestroy() {
        mTercoPlayer.stopPray();
        mTercoPlayer.releaseTercoPlayer();
        mTercoStateMachine.stopTerco();
        getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.btnContinue) {
			mTercoStateMachine.nextPray();
        }
    }

    @Override
    public void OnPrayChanged(Pray pray) {
       // mTercoPlayer.stopPray();
        tvPray.setText(pray.prayTextId());

        if(pray.prayOrder()>0) {

            tvTitle.setText(String.format(getString(pray.prayTitleId()), pray.prayOrder()));
        }
        else
        {
            tvTitle.setText(pray.prayTitleId());
        }

        if(mEnableSound) {
            mTercoPlayer.playPray(pray);
        }
    }

    @Override
    public void OnPrayFinished() {
		if(mListener != null) {
			mListener.onPrayFinished();
		}
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnRezarFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onPrayFinished();
    }

}
